package com.pkufinalproject.cleverreminder.controller;

import com.pkufinalproject.cleverreminder.hibernate.entity.RegularReminder;
import com.pkufinalproject.cleverreminder.hibernate.entity.User;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("regular")
public class RegularReminderController {
    private final static SessionFactory sf = new Configuration().configure().buildSessionFactory();

    /**
     * 创建新的定时提醒
     * @param title 定时提醒的名称
     * @param alertTime 下次提醒的时间，YYYY-MM-DD HH:MM:SS
     * @param alertInterval 提醒间隔时间，单位ms; 0表示不重复
     * @param ownerName 定时提醒的主人的用户名
     * @return 返回创建成功或失败的文字提醒
     */
    @RequestMapping("register")
    public static String registerRegularReminder(String title, String alertTime, int alertInterval, String ownerName) {
        User user = UserController.retrieveUser(ownerName);
        if (user == null) return "invalid owner name";
        RegularReminder regularReminder = retrieveRegularReminder(title, ownerName);
        if (regularReminder != null) return "regular reminder already exists";
        Session session = sf.openSession();
        regularReminder = new RegularReminder();
        regularReminder.setTitle(title);
        regularReminder.setAlertTime(Timestamp.valueOf(alertTime));
        regularReminder.setAlertInterval(alertInterval);
        regularReminder.setEnabled(true);
        regularReminder.setCreateTime(new Timestamp(System.currentTimeMillis()));
        regularReminder.setOwner(user);
        session.save(regularReminder);
        session.beginTransaction().commit();
        session.close();
        return "regular reminder registered successfully";
    }

    /**
     * 根据名称和用户将定时提醒disable掉
     * @param title 定时提醒的名称
     * @param ownerName 定时提醒的主人
     * @return 结果的文字说明
     */
    @RequestMapping("disable")
    public static String disableRegularReminder(String title, String ownerName) {
        Session session = sf.openSession();
        RegularReminder regularReminder = retrieveRegularReminder(title, ownerName);
        if (regularReminder == null) return "no request reminder found";
        regularReminder.setEnabled(false);
        session.update(regularReminder);
        session.beginTransaction().commit();
        session.close();
        return "disabled successfully";
    }

    /**
     * 根据名称和用户将定时提醒enable掉
     * @param title 定时提醒的名称
     * @param ownerName 定时提醒的主人
     * @return 结果的文字说明
     */
    @RequestMapping("enable")
    public static String enableRegularReminder(String title, String ownerName) {
        Session session = sf.openSession();
        RegularReminder regularReminder = retrieveRegularReminder(title, ownerName);
        if (regularReminder == null) return "no request reminder found";
        regularReminder.setEnabled(true);
        session.update(regularReminder);
        session.beginTransaction().commit();
        session.close();
        return "enabled successfully";
    }

    /***
     * 根据用户名找到用户的所有定时提醒
     * @param ownerName 用户名
     * @return json格式的定时提醒列表
     */
    @RequestMapping("retrieve_all")
    public static String strRetrieveAllRegularReminders(String ownerName) {
        List<RegularReminder> reminderList = retrieveAllRegularReminders(ownerName);
        return reminderList.toString();
    }

    public static List<RegularReminder> retrieveAllRegularReminders(String ownerName) {
        User user = UserController.retrieveUser(ownerName);
        if (user == null) return null;
        Session session = sf.openSession();
        Criteria criteria = session.createCriteria(RegularReminder.class)
                .add(Restrictions.like("owner", user));
        System.out.println("retrieve all " + ownerName);
        System.out.println(criteria.list());
        List<RegularReminder> reminderList = criteria.list();
        session.close();
        return reminderList;
    }

    @RequestMapping("retrieve")
    public static String strRetrieveRegularReminder(String title, String ownerName){
        RegularReminder regularReminder = retrieveRegularReminder(title, ownerName);
        if (regularReminder == null) return "no regular reminder found";
        return regularReminder.toString();
    }

    public static RegularReminder retrieveRegularReminder(String title, String ownerName) {
        RegularReminder regularReminder = null;
        User user = UserController.retrieveUser(ownerName);
        if (user == null) return null;
        Session session = sf.openSession();
        Criteria criteria = session.createCriteria(RegularReminder.class)
                .add(Restrictions.like("owner", user))
                .add(Restrictions.like("title", title));
        System.out.println("retrieve " + ownerName + " " + title);
        System.out.println(criteria.list());
        List<RegularReminder> reminderList = criteria.list();
        if (!reminderList.isEmpty()) regularReminder = reminderList.get(0);
        session.close();
        return regularReminder;
    }
}
